package com.lqw.vhr.excepion;

import com.lqw.vhr.model.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error(CustomizeErrorCode.DATABASE_ERROR_HAVING_RELATION_VALUE.getMessage());
        }
        return RespBean.error(CustomizeErrorCode.DATABASE_ERROR.getMessage());
    }

    @ExceptionHandler(CustomizeException.class)
    public RespBean customizeException(CustomizeException e) {
        return RespBean.error(e.getMessage());
    }
}
